from typing import Optional
from MyLindList.LeetCode_203 import ListNode

"""
    给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。
"""

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if head is None or head.next is None:
            return head
        pre = None
        left = head
        right = head.next
        while right:
            left.next = pre
            pre = left
            left = right
            right = right.next
        left.next = pre
        return left